#include <iostream>
#ifndef __SERVER_h__
#include "server.h"
#endif
using namespace std;
using namespace connector2;

void initService(int argc,char* argv[]){
	CosNaming::NamingContextExt_var inc;
	try{
		CORBA::ORB_var orb = CORBA::ORB_init(argc,argv);
		
		//activate ROOT POA
		CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
		PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
		PortableServer::POAManager_var mgr = poa->the_POAManager();
		mgr->activate();
	
		//get ref of NameService
		obj = orb->resolve_initial_references("NameService");
		inc = CosNaming::NamingContextExt::_narrow(obj);
		cout<<"Got NameService...."<<endl;

		BuyerToAgent_Imp buyertoagent_servant;
		connector2::BuyerToAgent_var t1 = buyertoagent_servant._this();
	
		CosNaming::Name name;
		name.length(1);
		name[0].id = CORBA::string_dup("BuyerToAgent_C++");
		inc->rebind(name,t1);
		cout<<"Agent server ready and wait..."<<endl;

		orb->run();

	}catch(const CORBA::Exception&){
		cerr<<"Uncaught CORBA exception."<<endl;
	}

}

int main(){
	int argc=4;                                           
    char** argv=new char*[4];                             
    argv[0]="-ORBid";                                     
    argv[1] = "omniORB4";                                 
    argv[2] = "-ORBInitRef";                              
    argv[3] = "NameService=corbaname::172.25.73.114:1060";
	initService(argc,argv);

	return 0;
}